package com.example.arithmeticleetcode.leetcode.jan;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: arithmetic-leetcode
 * @description:
 * @author: FangZhen
 * @create: 2021-01-07 14:28
 **/
public class Demo06 {

    public static void main(String[] args) {
        int[] arr1 = {1,2,3,4,5,10,6,7,8,9};
        System.out.println(canArrange(arr1, 5));
    }

    public static boolean canArrange(int[] arr, int k) {
        int[] mod = new int[k];
        for (int num : arr) {
            ++mod[(num % k + k) % k];
        }
        for (int i = 1; i + i < k; ++i) {
            System.out.println(i);
            if (mod[i] != mod[k - i]) {
                return false;
            }
        }
        return mod[0] % 2 == 0;
    }

    public boolean canArrange2(int[] arr, int k) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int num : arr) {
            int temp = (num % k + k) % k;
            map.put(temp, map.getOrDefault(temp, 0) + 1);
        }
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            int i = entry.getKey(), occ = entry.getValue();
            if (i > 0 && map.getOrDefault(k - i, 0) != occ) {
                return false;
            }
        }
        return map.getOrDefault(0, 0) % 2 == 0;
    }

    /**
     * 1528. 重新排列字符串
     *
     * @param s
     * @param indices
     * @return
     */
    public String restoreString(String s, int[] indices) {
        char[] chars = s.toCharArray();
        char[] retChars = new char[chars.length];
        for (int i = 0; i < chars.length; i++) {
            retChars[indices[i]] = chars[i];
        }
        return new String(retChars);
    }
}
